package com.mall.common.domain.manage;

import java.util.Iterator;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mall.b2c.util.SettingUtils;
import com.mall.common.domain.PriorityEntity;

/**
 * 部门实体
 * @author zhenglong
 */
@Entity
@Table(name="s_dept")
public class S_deptDomain extends PriorityEntity {
	
	private static final long serialVersionUID = 4712658186894543320L;
	
	private String name; // 部门名称
	private String code; // 编码
	@JsonIgnore private S_deptDomain upDomain;//上级部门
	private String link_man; //联系人
	private String link_phone; //联系电话
	private String description;
	@JsonIgnore private Set<S_deptDomain> children;
	
	@Transient
	public Long getParentId() { 
		S_deptDomain parent = getUpDomain();
		if (parent != null) {
			return parent.getId();
		} else {
			return null;
		}
	}
	
	/**
	 * 默认根部门为企业
	 * id=企业的id
	 * @return
	 */
	@Transient
	public static S_deptDomain genRoot(){
		S_deptDomain rootDept = new S_deptDomain();
		rootDept.setName(SettingUtils.get().getName());
		return rootDept;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false, length=30)
	public String getName() {
		return this.name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setUpDomain(S_deptDomain upDomain) {
		this.upDomain = upDomain;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "up_id")
	public S_deptDomain getUpDomain() {
		return this.upDomain;
	}

	@Column(length=255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "upDomain")
	@Cascade(value = { CascadeType.DELETE })
	@OrderBy("createDate desc")
	public Set<S_deptDomain> getChildren() {
		return children;
	}

	public void setChildren(Set<S_deptDomain> children) {
		this.children = children;
	}

	public String getLink_man() {
		return link_man;
	}

	public void setLink_man(String link_man) {
		this.link_man = link_man;
	}

	public String getLink_phone() {
		return link_phone;
	}

	public void setLink_phone(String link_phone) {
		this.link_phone = link_phone;
	}

	@PrePersist
	public void prePersist(){
		if(this.getPriority() == null){
			this.setPriority(10);
		}
	}
	
	@PreRemove
	public void preremove(){
		Set<S_deptDomain> children = getChildren();
		if (children != null) {
			for (Iterator<S_deptDomain> iterator = children.iterator(); iterator.hasNext(); ){
				S_deptDomain dept = (S_deptDomain)iterator.next();
				dept.setUpDomain(null);
			}
		}
	}

}
